package com.cptravel.activity.index;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseFragment;
import com.core.eventbus.EventBus;
import com.core.eventbus.EventBus.SubscriberChangeListener;
import com.core.util.B5MActivityHelper;
import com.core.util.B5MPreferenceHelper;
import com.core.util.B5MPreferenceKeys;
import com.core.widget.autoscrollviewpager.AutoScrollViewPager;
import com.core.widget.viewpagerindicator.CirclePageIndicator;
import com.cptravel.R;
import com.cptravel.activity.LoginActivity;
import com.cptravel.adapter.IndexPagerAdapter;
import com.cptravel.api.IndexSlideRequest;
import com.cptravel.api.IndexSlideResponse;
import com.cptravel.api.LoginRequest;
import com.cptravel.api.LoginResponse;
import com.cptravel.tool.EventNames;
import com.cptravel.tool.UserInfoManage;
import org.json.JSONException;
import org.json.JSONObject;

public class IndexFragment extends B5MBaseFragment implements SubscriberChangeListener
{
    private AutoScrollViewPager mPager;
    private CirclePageIndicator mIndicator;
    private IndexPagerAdapter mIndexPagerAdapter;
    private IndexSlideRequest mIndexSlideRequest;
    private LoginRequest mLoginRequest;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.index;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        bt_left = (Button) containerView.findViewById(R.id.bt_head_left);
        bt_right = (Button) containerView.findViewById(R.id.bt_head_right);
        tv_title = (TextView) containerView.findViewById(R.id.tv_head_title);
        tv_title.setText("首页");
        bt_right.setVisibility(View.VISIBLE);
        bt_left.setVisibility(View.GONE);
        bt_right.setText("登录");
        bt_right.setBackgroundDrawable(null);
        mProgressBar = (ProgressBar) containerView.findViewById(R.id.pb_head);
        mIndexPagerAdapter = new IndexPagerAdapter(getFragmentManager());
        mPager = (AutoScrollViewPager) containerView.findViewById(R.id.pager);
        mPager.setAdapter(mIndexPagerAdapter);
        mPager.setInterval(3000);
        mIndicator = (CirclePageIndicator) containerView.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        bt_right.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                if (!UserInfoManage.getInstance().isLogined())
                {
                    B5MActivityHelper.startActivity(mActivity, LoginActivity.class);
                }
                else
                {
                    UserInfoManage.getInstance().clearData();
                    bt_right.setText("登录");
                }
            }
        });
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mIndexSlideRequest = new IndexSlideRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                IndexSlideResponse indexSlideResponse = (IndexSlideResponse) b5mBaseResponse;
                mIndexPagerAdapter.pics.clear();
                mIndexPagerAdapter.pics.addAll(indexSlideResponse.pics);
                mIndexPagerAdapter.notifyDataSetChanged();
                mPager.startAutoScroll();
            }
        }, mProgressBar);
        //
        boolean isAutoLogin = B5MPreferenceHelper.readBoolValue(mContext, B5MPreferenceKeys.AUTO_LOGIN);
        if (isAutoLogin)
        {
            String username = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USER_USERID);
            String pwd = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USER_PWD);
            if (TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd))
            {
                return;
            }
            mLoginRequest = new LoginRequest(new ApiListenrDefaultImpl()
            {
                @Override
                public void onSuccess(B5MBaseResponse b5mBaseResponse)
                {
                    // TODO Auto-generated method stub
                    super.onSuccess(b5mBaseResponse);
                    LoginResponse loginResponse = (LoginResponse) b5mBaseResponse;
                    JSONObject jsonObject = new JSONObject();
                    try
                    {
                        jsonObject.put("userid", loginResponse.userid);
                    }
                    catch (JSONException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    bt_right.setText("注销");
                    UserInfoManage.getInstance().saveUser(jsonObject.toString());
                }
            }, mProgressBar);
            mLoginRequest.userid = username;
            mLoginRequest.pwd = pwd;
            mLoginRequest.start();
        }
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        EventBus.getInstance().subscribe(EventNames.LOGIN_SUCCESS, this);
        if (mIndexPagerAdapter.pics.isEmpty() && !mIndexSlideRequest.isStarted())
        {
            mIndexSlideRequest.start();
        }
    }

    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        EventBus.getInstance().unSubscribe(EventNames.LOGIN_SUCCESS, this);
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName, Object notificateContent)
    {
        // TODO Auto-generated method stub
        if (EventNames.LOGIN_SUCCESS.equals(notificationName))
        {
            bt_right.setText("注销");
        }
    }
}
